package karaoke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Sala;
import karaoke.util.ConexionBD;

public class SalaDAO extends BaseDAO{

	
	public Sala insertar(Sala vo) throws DAOExcepcion {
		String query = "insert into tb_sala(sala_nombre, sala_capacidad, sala_descripcion, BD_Local_local_Id) values (?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getSala_Nombre());
			stmt.setInt(2, vo.getSala_Capacidad());
			stmt.setString(3, vo.getSala_Descripcion());
			stmt.setInt(4, vo.getLocal_Id());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			/*
			 * // Obtener el ultimo id int id = 0; query =
			 * "select last_insert_id()"; stmt = con.prepareStatement(query); rs
			 * = stmt.executeQuery(); if (rs.next()) { id = rs.getInt(1); }
			 * vo.setLocal_Id(id);
			 */
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	
	public void eliminar(int sala_id) throws DAOExcepcion {
		String query = "delete from tb_sala WHERE sala_Id=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, sala_id);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
}
